#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_system.h"
#include "lvgl.h"
#include "bsp_board.h" //1.方案板参数配置初始化必须包含的头文件
#include "lvgl_init.h" //2.方案板LVGL接口初始化必须包含的头文件
#include "wav_play.h"
#include "nvs_flash.h"
#include "espnow_lite.h"
#include "lv_example_scroll.h"
#include "uart_rs485.h" //引入rs485头文件,通信波特率9600
static const char *TAG = "main";

void espnow_start()
{
    uint8_t broadcast_mac[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

#if 0
    esp_now_peer_info_t* peer = malloc(sizeof(esp_now_peer_info_t));
    espnow_payload_t* send_data = malloc(sizeof(espnow_payload_t));
#else
    esp_now_peer_info_t *peer = heap_caps_malloc(sizeof(esp_now_peer_info_t), MALLOC_CAP_SPIRAM);
    espnow_payload_t *send_data = heap_caps_malloc(sizeof(espnow_payload_t), MALLOC_CAP_SPIRAM);
#endif
    // Initialize NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
    {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }

    ESP_ERROR_CHECK(ret);
    espnow_wifi_init();
    espnow_init();
}
void uart0_rx_task()
{
    uint8_t *data = (uint8_t *)malloc(RX_BUF_SIZE + 1);
    while (1)
    {
        // 获取RS485接收的数据
        const int rxBytes = uart_read_bytes(UART_NUM_0, data, RX_BUF_SIZE, 10);
        if (rxBytes > 0)
        {

            data[rxBytes] = 0;
            ESP_LOGI(TAG, "%s\r\n", (char *)data); // usb口打印
            // // 将接收到的数据发出去
            uart_write_bytes(UART_NUM_0, (char *)data, rxBytes);

            // // 要发送的16进制指令
            // uint8_t hex_cmd[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A};

            // // 发送16进制指令
            // uart_write_bytes(UART_NUM_0, (const char *)hex_cmd, sizeof(hex_cmd));
        }
    }
    free(data);
}

#define LED_PIN 44
void LED_task()
{
    esp_rom_gpio_pad_select_gpio(LED_PIN);
    gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
    gpio_set_level(LED_PIN, 0);
    while (1)
    {
        gpio_set_level(LED_PIN, 0);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        gpio_set_level(LED_PIN, 1);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}
void app_main()
{

    esp_vfs_spiffs_conf_t conf = {
        .base_path = "/spiffs",
        .partition_label = NULL,
        .max_files = 10,
        .format_if_mount_failed = true};
    esp_err_t ret = esp_vfs_spiffs_register(&conf);

    if (ret != ESP_OK)
    {
        if (ret == ESP_FAIL)
        {
            ESP_LOGE(TAG, "Failed to mount or format filesystem");
        }
        else if (ret == ESP_ERR_NOT_FOUND)
        {
            ESP_LOGE(TAG, "Failed to find SPIFFS partition");
        }
        else
        {
            ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
        }
        return;
    }

    // 1.方案板参数配置初始化
    sys_int();
    // 2.方案板LVGL接口初始化
    lvgl_init();
    // 3.LVGL应用层,用户程序
    uart_init(UART_NUM_0); // rs485串口初始化
    // rs485接收任务
    xTaskCreate(uart0_rx_task, "uart0_rx_task", 4096, NULL, 4, NULL);
    xTaskCreate(LED_task, "LED_task", 4096, NULL, 3, NULL);
    lv_port_sem_take();
    lv_example_86box();
    lv_port_sem_give();
    play_user_wav("welcome.wav");
    espnow_start();
}
